import pandas.io.clipboard as cb
import sys
import threading
import time
import os
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0"
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog,QWidget
from PyQt5.QtCore import Qt , QPoint
from PyQt5.QtGui import QCloseEvent
import numpy as np
import json
import math
from multiprocessing import Pool,Process,Value,Array
import logging
from logging import handlers
fname=r"C:\Users\yanqi\Desktop\rbgame\single\data\gs\11.jpg"
class clickableQlabel(QtWidgets.QLabel):
    corners=[]
    def __init__(self, parent=None):
        super(clickableQlabel, self).__init__(parent)
        frame=cv2.imread(fname)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
        h,s,v=cv2.split(hsv)
        mask = cv2.inRange(hsv, np.array([90,0,0]), np.array([180,255,80]))
        self.corners = cv2.goodFeaturesToTrack(gray, 3000, 0.05, 4)
        print(self.corners)
        for pt in self.corners:
            # print(pt)
            x = np.int32(pt[0][0])
            y = np.int32(pt[0][1])
            cv2.circle(frame, (x, y), 5, (0, 255, 0), 2)
        img = QtGui.QImage(frame.data, frame.shape[1], frame.shape[0], QtGui.QImage.Format_BGR888)
        self.setPixmap(QtGui.QPixmap.fromImage(img))
    def mousePressEvent(self, event) :   
    # 鼠标左键按下
        if event.button() == Qt.LeftButton :
            p = [event.pos().x(),event.pos().y()]
            index=0
            for i in range(len(self.corners)):
                if abs(p[0]-self.corners[i][0][0])+abs(p[1]-self.corners[i][0][1])<abs(p[0]-self.corners[index][0][0])+abs(p[1]-self.corners[index][0][1]):
                    index=i
            if abs(p[0]-self.corners[index][0][0])+abs(p[1]-self.corners[index][0][1])<=10:
                pos=self.corners[index][0]
            else:
                pos=p
            cb.copy(f"[{round(pos[0])},{round(pos[1])}],")
            print(cb.paste())
            frame=cv2.imread(fname)
            for pt in self.corners:
                # print(pt)
                x = np.int32(pt[0][0])
                y = np.int32(pt[0][1])
                cv2.circle(frame, (x, y), 5, (0, 255, 0), 2)
            cv2.circle(frame, (round(pos[0]), round(pos[1])), 3, (0, 0, 255), 2)
            img = QtGui.QImage(frame.data, frame.shape[1], frame.shape[0], QtGui.QImage.Format_BGR888)
            self.setPixmap(QtGui.QPixmap.fromImage(img))
class Ui_Detect(QWidget):
    def __init__(self):
        super().__init__()

        self.label = clickableQlabel(self)
        self.label.setText("")
        self.label.setObjectName("label")
        self.label.move(0,0)
        self.label.show()
        self.label.resize(1920,1080)
        
        self.move(0,0)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QWidget()
    ui = Ui_Detect()
    ui.show()
    sys.exit(app.exec_())

